/*
 * @Author: zhuxiaoyi
 * @Date: 2023-12-28 17:21:59
 * @LastEditor: zhuxiaoyi
 * @LastEditTime: 2024-01-23 09:58:37
 * @Description:
 */
import { Inject, Controller, Get, Query, Post, Body} from '@midwayjs/core';
import { Context } from '@midwayjs/koa';
import { TaskService } from '../service/task.service';
import { CreateCatDto } from '../Dto/crateTaskDao';
import { ApiQuery } from '@midwayjs/swagger';
const path = require('path');

@Controller('/api')
export class APIController {
  @Inject()
  ctx: Context;

  @Inject()
  taskService: TaskService;

  @Get('/taskInfo')
  @ApiQuery({
    name: 'taskId',
  })
  async getTask(@Query('taskId') taskId) {
    console.log(taskId);
    let task = await this.taskService.queryTask(taskId);
    console.log(task);
    const fileInfo = path.parse(task.imageFile);
    console.log(fileInfo);
    if (task.progress == 100) {
      return {
        success: true,
        message: 'OK',
        data: {
          isFinsh:"true",
          progress: task.progress,
          resultPath:`${fileInfo.dir}/${fileInfo.name}_501/${fileInfo.name}_501_seg_cls${fileInfo.ext}`
        },
      };
    } else {
      return {
        success: true,
        message: 'OK',
        data: {
          progress: task.progress,
          isFinsh:"false"
        },
      };
    }
  }

  @Post('/createTask')
  async createTask(@Body() dto: CreateCatDto) {
    const task = await this.taskService.createTask(dto);
    return {
      success: true,
      message: 'OK',
      data: {
        taskId: task.id,
      },
    };
  }
}
